package com.ccl.controller.client;

import com.ccl.entity.Comment;
import com.ccl.entity.ResponseData;
import com.ccl.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/comments/publish")
    @ResponseBody
    public ResponseData publish(HttpServletRequest request,@RequestParam("aid") Integer aId,@RequestParam("text") String content){//文章id/评论内容
        //发表评论
        //创建评论对象
        Comment comment = new Comment();
        //制定评论属性从哪来
        comment.setArticleId(aId);
        comment.setCreated(new Date());
        String ip = request.getRemoteAddr();
        comment.setIp(ip);
        comment.setContent(content);
        comment.setStatus("approved");
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        comment.setAuthor(user.getUsername());
        //发表评论
        commentService.pushComment(comment);
        //返回评论成功
        return ResponseData.OK();
    }
}
